/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: message.cpp
 *
 * Purpose: message and message queue imeplementation for 'COM' Functional
 *
 * Developer:
 *   wen.gu , 2020-07-29
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "panda/com/runtime/message.h"

#include <queue>  
#include <condition_variable>

namespace panda
{
namespace com
{
namespace runtime
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/


using MsgQueue = std::queue<MessagePtr>;

class MessageQueue::impl
{
public:
    bool mIsEnableWait = true;
    MsgQueue mMsgQueue;
    std::condition_variable mCond;
    std::mutex mLock;
};
/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
MessageQueue::MessageQueue()
    :mImpl(new impl)
{
    /** todo something */
}

MessageQueue::~MessageQueue()
{
    /** todo something */
}

void MessageQueue::push(MessagePtr msg)
{
    panda::core::AutoLock al(mImpl->mLock);
    mImpl->mMsgQueue.push(msg);

    if (mImpl->mMsgQueue.size() == 1)
    {
        mImpl->mCond.notify_all();
    }
}

MessagePtr MessageQueue::popAndWait()
{
    panda::core::AutoLock al(mImpl->mLock);
    MsgQueue& mq = mImpl->mMsgQueue;

    while (mImpl->mIsEnableWait && mq.empty())
    {
        mImpl->mCond.wait(al);
    }
    MessagePtr msg = nullptr;

    if (!mq.empty())
    {
        msg = mq.front();
        mq.pop();
    }

    return msg;

}

void MessageQueue::enableWait()
{
    panda::core::AutoLock al(mImpl->mLock);
    mImpl->mIsEnableWait = true;
}

void MessageQueue::disableWait()
{
    panda::core::AutoLock al(mImpl->mLock);
    mImpl->mIsEnableWait = false;
    mImpl->mCond.notify_all();
}


} /** namespace runtime */
} /** namespace com */
} /** namespace panda */


